﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;
using DAL.Models;
using BAL;
using BAL.Interfaces;
using BibliotecaVirtual.Models;

namespace BibliotecaVirtual.Controllers
{ 
    public class MaterialsController : Controller
    {
        private IMaterialsService BALMaterials = null;
        private IAutorsService BALAutors = null;
        private IEditorialsService BALEditorials = null;
        private ITipoMaterialsService BALTipoMaterials = null;
        private ITemasService BALTemas = null;

        public MaterialsController(IMaterialsService MaterialsService,
                                   IAutorsService AutorsService,
                                   IEditorialsService EditorialsService,
                                   ITipoMaterialsService TipoMaterialsService,
                                   ITemasService TemasService)
        {
            BALMaterials = MaterialsService;
            BALAutors = AutorsService;
            BALEditorials = EditorialsService;
            BALTipoMaterials = TipoMaterialsService;
            BALTemas = TemasService;
        }

        //
        // GET: /Materials/
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ViewResult Index()
        {
            return View(BALMaterials.GetAllValidados().ToList());
        }

        //
        // GET: /Material/
        //[Authorize(Roles = "ADMINISTRADOR")]
        public ViewResult IndexDetallado(long id)
        {
            Materials material = BALMaterials.GetByID(id); 
            return View(material);
        }

        //
        // GET: /Materials/Details/5
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ViewResult Details(long id)
        {
            object[] keys = new object[1] { id };
            return View(BALMaterials.Find(keys));
        }

        //
        // GET: /Materials/Create
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Create()
        {
            cargarDatos(null); 
            return View();
        } 

        //
        // POST: /Materials/Create

        [HttpPost]
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Create(Materials item)
        {
            if (ModelState.IsValid)
            {
                BALMaterials.Create(item);
                return RedirectToAction("Index");  
            }
            cargarDatos(item); 
            return View(item);
        }
        
        //
        // GET: /Materials/Edit/5
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Edit(long id)
        {
            object[] keys = { id };
            Materials item = BALMaterials.Find(keys);
            cargarDatos(item);
            return View(item);
        }

        //
        // POST: /Materials/Edit/5

        [HttpPost]
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Edit(Materials item)
        {
            if (ModelState.IsValid)
            {
                BALMaterials.Edit(item);
                return RedirectToAction("Index");
            }
            cargarDatos(item);
            return View(item);
        }

        //
        // GET: /Materials/Delete/5
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Delete(long id)
        {
            object[] keys = new object[1] { id };
            return View(BALMaterials.Find(keys));
        }

        //
        // POST: /Materials/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult DeleteConfirmed(long id)
        {
            object[] keys = new object[1] { id };
            Materials item = BALMaterials.Find(keys);
            BALMaterials.Remove(item);
            return RedirectToAction("Index");
        }

        //protected override void Dispose(bool disposing)
        //{
        //    BALMaterials.Dispose();
        //    base.Dispose(disposing);
        //}

        public ActionResult Buscar()
        {
            ViewBag.Tema = new SelectList(BALTemas.GetAll(), "id", "Nombre");
            return View();
        }

        [HttpPost]
        public ActionResult Buscar(Buscar model)
        {
            IQueryable<Materials> result = BALMaterials.Buscar(model);
            if (result.Count<Materials>() > 0)
            {
                return View("MostrarResultados", result);
            }
            else
            {
                return View("Buscar_sin_datos");
            }
        }

        protected void cargarDatos(Materials material)
        {
            try
            {
                if (material != null)
                {
                    ViewBag.AutorId = new SelectList(BALAutors.GetAll(), "id", "Nombre", material.AutorId);
                    ViewBag.EditorialId = new SelectList(BALEditorials.GetAll(), "id", "Nombre", material.EditorialId);
                    ViewBag.TipoMaterialId = new SelectList(BALTipoMaterials.GetAll(), "id", "Descripcion", material.TipoMaterialId);
                }
                else
                {
                    ViewBag.AutorId = new SelectList(BALAutors.GetAll(), "id", "Nombre");
                    ViewBag.EditorialId = new SelectList(BALEditorials.GetAll(), "id", "Nombre");
                    ViewBag.TipoMaterialId = new SelectList(BALTipoMaterials.GetAll(), "id", "Descripcion");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}